//
//  SXStatusCell.swift
//  微博09项目
//
//  Created by mac on 15/9/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
let SXStatusCellMargin : CGFloat = 12
let SXStatusIconWidth : CGFloat = 35
/// 默认的图片大小
let SXStatusPictureItemWidth: CGFloat = 90
/// 默认的图片间距
let SXStatusPictureItemMargin: CGFloat = 10
/// 每行的最大图片数量
let SXStatusPictureMaxCount: CGFloat = 3
/// 配图视图的最大的尺寸
let SXStatusPictureMaxWidth = SXStatusPictureMaxCount * SXStatusPictureItemWidth + (SXStatusPictureMaxCount - 1) * SXStatusPictureItemMargin
class SXStatusCell: UITableViewCell {
    var pictureViewWidthCons:NSLayoutConstraint?
    var pictureViewHeightCons:NSLayoutConstraint?
    var pictureViewTopCons:NSLayoutConstraint?
    //添加模型
    var statusViewModel:SXStatusViewModel?{
        didSet{
            topView.statusViewModel = statusViewModel
            contentLabel.text = statusViewModel?.status.text
            //在系统自动布局中，随机修改表格约束，使用自动计算行高，很容易出问题
            //通过测试提前确定方法的可行性
           // pictureViewWidthCons?.constant = SXStatusPictureItemWidth * CGFloat(random() % 4)
            //设置配图，内部的sizeTofit()
            pictureView.statusViewModel = statusViewModel
            pictureViewWidthCons?.constant = pictureView.bounds.width
            pictureViewHeightCons?.constant = pictureView.bounds.height
            //判断有没有配图，如果有就返回SXStatusCellMargin 0
            pictureViewTopCons?.constant = statusViewModel?.thumbnailURLs?.count == 0 ? 0:SXStatusCellMargin
        }
    }
    //设置行高
    func rowHeight(viewModel: SXStatusViewModel) ->CGFloat{
        statusViewModel = viewModel
        //跟新约束
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    //搭建界面
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加控件
    func setUpUI(){
        //设置图片的填充颜色，一般不建议使用UIColorClear 也不主张使用alpha
        backgroundColor = UIColor.whiteColor()
        //设置分隔
        let SetTopView = UIView()
        SetTopView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        //1，设置控件
        contentView.addSubview(topView)
        contentView.addSubview(SetTopView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        //2.设置自动布局
        let width = UIScreen.mainScreen().bounds.width
        
        /// 1，顶部分隔视图
        SetTopView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 10))
        
       /// 2，顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: width, height: 50),offset:CGPoint(x: 0, y: 10))
        
        /// 3，文本视图
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: SXStatusCellMargin, y: SXStatusCellMargin))
        
       /// 4，配图视图

        //print(pictureViewHeightCons)
        /// 5，底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size:CGSize(width: width, height: 44),offset:CGPoint(x: -SXStatusCellMargin, y: SXStatusCellMargin))
       // bottomView.ff_AlignInner(type: ff_AlignType.BottomLeft, referView: contentView, size: nil)
    }
    /// 1,顶部视图
    private lazy var topView:SXStatusCellTopView = SXStatusCellTopView()
   ///2,文本标签
   lazy var contentLabel = UILabel(title: nil, color: UIColor.darkGrayColor(), fontSize: 15, layoutWidth: UIScreen.mainScreen().bounds.width - 2 * SXStatusCellMargin)
    /// 3,配图视图
   lazy var pictureView:SXStatusPictureView = SXStatusPictureView()
   
  ///4,底部视图
   lazy var bottomView:SXStatusCellBottomView = SXStatusCellBottomView()
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }

}
